<?php
$conn = mysqli_connect('localhost' , 'root' , 'weicedbmojito160824','weixin_didao_wxapp');
$query = "Set Names 'utf8mb4'";
mysqli_query($conn,$query);
if(isset($_GET['partyId']) && isset($_GET['title']) && isset($_GET['beginTime']) && isset($_GET['maxMember']) && isset($_GET['location']) && isset($_GET['remindPicker']) && $_GET['jyj']==='jyj'){
    $partyId = $_GET['partyId'];
    $formId = $_GET['formId'];
    $title = $_GET['title'];
    $beginTime = $_GET['beginTime'];
    $beginTimeUnix = $_GET['beginTimeUnix'];
    $maxMember = $_GET['maxMember'];
    $location = $_GET['location'];
    $location_str = json_encode($location,JSON_UNESCAPED_UNICODE);
    $remindPicker = $_GET['remindPicker'];
    isset( $_GET['description'])? $_GET['description']!='undefined'? $description= $_GET['description']:$description = '':$description = '';
    $userInfo = $_GET['userInfo'];
    $openid = json_decode($userInfo,true)['openId'];
    if ($beginTimeUnix+5>time()){//加10s缓冲
        if($partyId==='-1') {
            $query = "INSERT INTO didao_wxapp_party (`openid`,`location`,`begin_time`,`begin_time_unix`,`name`,`description`,`max_member`,`create_time`,`update_time`)
              VALUES ('$openid','$location_str','$beginTime','$beginTimeUnix','$title','$description','$maxMember',NOW(),NOW());
";
            $result = mysqli_query($conn, $query);
            if ($result) {
                $partyId = mysqli_insert_id($conn);
                $query = "INSERT INTO didao_wxapp_party_attender (`party_id`,`form_id`,`is_remind`,`openid`,`description`,`remind_picker`,`attender_num`,`create_time`,`update_time`)
                  VALUES ('$partyId','$formId','0','$openid','','$remindPicker',0,NOW(),NOW())";
                $result = mysqli_query($conn, $query);
                echo json_encode(array(
                    'errCode' => 1,
                    'errMsg' => '开启成功',
                    'partyId' => $partyId,
                    'query' => $query
                ), JSON_UNESCAPED_UNICODE);
            } else {
                echo json_encode(array(
                    'errCode' => -1,
                    'errMsg' => '开启失败',
                    'partyId' => -1
                ), JSON_UNESCAPED_UNICODE);
            }
        }
        elseif($partyId > 0 ){
            $query = " SELECT SUM(attender_num) AS attender_num_sum  FROM didao_wxapp_party_attender WHERE party_id = '$partyId'  AND cancel_flag = 1";
            $attenderNumSumResult = mysqli_query($conn, $query);
            $attenderNumSum = mysqli_fetch_assoc($attenderNumSumResult)['attender_num_sum'];

            if ($maxMember > $attenderNumSum){
                $query = "UPDATE didao_wxapp_party SET 
              `openid`='$openid',`location`='$location_str',`begin_time`='$beginTime',`begin_time_unix`='$beginTimeUnix',`name`='$title',`description`='$description',
              `max_member`='$maxMember',`update_time`=NOW() WHERE id = '$partyId'";
                $result = mysqli_query($conn, $query);
                if ($result) {
                    $query = "UPDATE  didao_wxapp_party_attender SET 
                  `remind_picker`='$remindPicker',`update_time` = NOW(),`form_id`='$formId',`is_remind` = '0'
                   WHERE party_id = '$partyId' AND openid = '$openid'";
                    $result = mysqli_query($conn, $query);
                    echo json_encode(array(
                        'errCode' => 1,
                        'errMsg' => '修改成功',
                        'partyId' => $partyId
                    ), JSON_UNESCAPED_UNICODE);
                } else {
                    echo json_encode(array(
                        'errCode' => -1,
                        'errMsg' => '修改失败',
                        'partyId' => -1
                    ), JSON_UNESCAPED_UNICODE);
                }
            }else{
                echo json_encode(array(
                    'errCode' => -1,
                    'errMsg' => '当前活动已有' . ($attenderNumSum) . '人参与',
                    'partyId' => -1
                ), JSON_UNESCAPED_UNICODE);
            }
        }
    }else{
        echo 2;
        echo json_encode(array(
            'errCode' => -1,
            'errMsg' => '时间有误',
            'partyId' => -1
        ), JSON_UNESCAPED_UNICODE);
    }


}
/**
 * Created by PhpStorm.
 * User: a7849
 * Date: 2017/7/14
 * Time: 16:39
 */